<%--
    Document   : index
    Created on : 19 juin 2010, 23:51:05
    Author     : marya
--%>

<%@page contentType="text/xml" pageEncoding="UTF-8"%>
<%@page import="org.restlet.resource.ClientResource" %>
<%@page import="org.restlet.representation.Representation" %>
<%@page import="org.restlet.ext.xml.DomRepresentation" %>
<%@page import="org.restlet.Response" %>
<%@page import="org.restlet.Request" %>
<%@page import="org.restlet.data.Method" %>
<%@page import="org.restlet.data.*" %>
<%@page import="org.w3c.dom.Element" %>
<%@page import="org.w3c.dom.Node" %>
<%@page import="org.w3c.dom.NodeList" %>
<%@page import="java.util.List" %>
<%@page import="java.util.Map" %>
<%@page import="xmlws.helpers.WebService" %>
<%

            ClientResource client = new ClientResource(WebService.federateur + "/promotions/" + request.getParameter("idPromotion"));

            if (request.getMethod().equals("GET")) {
                DomRepresentation representation = new DomRepresentation(client.get());
                NodeList elements = representation.getDocument().getElementsByTagName("hcard");
                if (elements.getLength() > 0) {
                    for (int i = 0; i < elements.getLength(); i++) {
                        Node node = elements.item(i);
                        out.println(node.getTextContent());
                    }
                }
            } else if (request.getMethod().equals("PUT")) {

                Form form = new Form();

                form.add("nomPromotion", request.getParameter("nomPromotion"));
                form.add("uriPromotion", request.getParameter("uriPromotion"));

                client.put(form.getWebRepresentation());

                if (client.getResponse().getStatus().equals(Status.SUCCESS_OK)) {
                    response.setStatus(200);
                    out.println("<span class=\"result\">Promotion modifié</span>");
                } else {
                    response.setStatus(client.getResponse().getStatus().getCode());
                    out.println(client.getResponseEntity().getText());
                }


                
            } else if (request.getMethod().equals("DELETE")) {

                client.delete();
                if (client.getResponse().getStatus().equals(Status.SUCCESS_NO_CONTENT)) {
                    out.println("<span class=\"result\">Promotion supprimée</span>");
                } else {
                    out.println(client.getResponseEntity().getText());
                }

            }

%>

